﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static MyMusicPlay._4._0.Media;

namespace MyMusicPlay._4._0
{
    public class MusicPlaying : BaseClass
    {
        public MainWindow MainWindow { get { return PlayApplication.GetMainWindow(); } }
        public Media Media { get { return PlayApplication.GetModule<Media>(); } }

        public void Initialize()
        { }

        /// <summary>
        /// 加载并显示歌单[包括渲染]
        /// </summary>
        /// <param name="listName"></param>
        public void LoadMusisListToDisPlay(string listName)
        {
            var medio = PlayApplication.GetModule<Media>();
            if (medio.MusicListName == listName)
                return;

            medio.MusicListName = listName;
            medio.CurDisplayList.Clear();

            var listPath = medio.GetMusicListPathByName(listName);

            ReadMusic readMusic = new();
            var list = readMusic.ReadMusicList(listPath, listName);

            medio.CurDisplayList = list;

            PlayApplication.GetModule<Rander>().RenderViewOfMusicList(list.ToLists());
        }

        /// <summary>
        /// 刷新指定歌曲数据[包括面板]
        /// </summary>
        public void FlushedSelectMusic(Music newMusic)
        {
            //刷新显示歌单数据
            if (Media.CurDisplayList.ContainsKey(newMusic.Key))
            {
                Media.CurDisplayList[newMusic.Key] = newMusic;
            }
            //刷新显示歌单Item
            if (!MainWindow.Retrieval_Judge)
                MainWindow.MusicDisplayList.Items[newMusic.Key] = new { Code = newMusic.Key, Name = newMusic.Name, Author = newMusic.Singer };
            //刷新播放
            if (Media.CurPlayingList.ContainsKey(newMusic.Key))
            {
                //刷新播放歌单数据
                if (Media.CurPlayingList[newMusic.Key].MusicListName == newMusic.MusicListName)
                {
                    Media.CurPlayingList[newMusic.Key] = newMusic;
                }
                //刷新播放显示Item
                var fsctory = PlayApplication.GetModule<ElementFactoryBase>();
                (fsctory.Get<TextBlock>("MusicPlayListByName_" + newMusic.Key)[0] as TextBlock).Text = newMusic.Name;
                (fsctory.Get<TextBlock>("MusicPlayListBySinger_" + newMusic.Key)[0] as TextBlock).Text = newMusic.Singer;
            }

            //刷新播放显示Top[目前没有实现]

            //[检索状态需要额外处理一下]
        }


        /// <summary>
        /// 下一首
        /// </summary>
        public void NextMusicPlaying()
        {
            var index = Media.CurPlayingKey;
            index = GetNextMusic(index);
            Media.MusicPlaying(index);
        }

        /// <summary>
        /// 上一首
        /// </summary>
        public void LastMusicPlaying()
        {
            var index = Media.CurPlayingKey;
            index = GetLastMusic(index);
            Media.MusicPlaying(index);
        }

        /// <summary>
        /// 播放 / 暂停
        /// </summary>
        public void Playing()
        {
            switch (Media.playState)
            {
                case Media.PlayState.Playing:
                    Media.TimeOut();
                    PlayApplication.GetModule<ScrollToPlaying>().TimeOutAnim();
                    break;
                case Media.PlayState.TimeOut:
                    Media.MusicPlaying();
                    PlayApplication.GetModule<ScrollToPlaying>().StateByTimeOut();
                    break;
                case Media.PlayState.Stop:
                    break;
                case Media.PlayState.TimeOuting:
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 删除当前播放的音乐
        /// </summary>
        public void DelCurPlayingMusic(int key)
        {
            if (Media.CurPlayingList.ContainsKey(key))
            {
                var curKey = key;
                //先指向下一首歌曲
                if (key == Media.CurPlayingKey)
                {
                    NextMusicPlaying();
                }
                Media.CurPlayingList.Remove(curKey);
                Media.RemovePlayOrderList(curKey);
            }
        }

        /// <summary>
        /// 获取下一首歌曲下标
        /// </summary>
        public int GetNextMusic(int key)
        {
            var orderList = Media.GetPlayOrderList();
            if (orderList.Count == 1)
            {
                return key;
            }
            var curIndex = orderList.IndexOf(key);
            if (curIndex >= orderList.Count() - 1)
            {
                curIndex = -1;
            }
            curIndex++;
            return orderList[curIndex];
        }

        /// <summary>
        /// 获取上一首歌曲下标[此处是用下标的]
        /// </summary>
        public int GetLastMusic(int key)
        {
            var orderList = Media.GetPlayOrderList();
            if (orderList.Count == 1)
            {
                return key;
            }
            var curIndex = orderList.IndexOf(key);
            if (curIndex <= 0)
            {
                curIndex = orderList.Count();
            }
            curIndex--;
            return orderList[curIndex];
        }

    }
}
